package com.yf.exam.modules.trainingarchives.studentinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.StudentAssessmentExportDTO;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.StudentInfoExportDTO;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.three.StudentInfoListExportDTO;
import com.yf.exam.modules.trainingarchives.studentinfo.dto.export.StudentExportDTO;
import com.yf.exam.modules.trainingarchives.studentinfo.dto.req.StudentInfoReqDTO;
import com.yf.exam.modules.trainingarchives.studentinfo.dto.resp.StudentInfoRespDTO;
import com.yf.exam.modules.trainingarchives.studentinfo.entity.StudentInfo;
import com.yf.exam.modules.trainingarchives.studentinfo.mapper.StudentInfoMapper;
import com.yf.exam.modules.trainingarchives.studentinfo.service.IStudentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.exam.modules.user.UserUtils;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学员信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo> implements IStudentInfoService {

    @Override
    public Boolean add(StudentInfoReqDTO studentInfoReqDTO) {
        StudentInfo studentInfo = new StudentInfo();
        BeanUtils.copyProperties(studentInfoReqDTO,studentInfo);
        String userName = UserUtils.getUserName();
        studentInfo.setCreatedBy(userName);
        return this.save(studentInfo);
    }

    @Override
    public Boolean deleteById(String id) {
        LambdaQueryWrapper<StudentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentInfo::getId,id);
        return this.remove(wrapper);
    }

    @Override
    public Boolean deleteBatch(List<String> ids) {
        LambdaQueryWrapper<StudentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(StudentInfo::getId,ids);
        return this.remove(wrapper);
    }

    @Override
    public Boolean updateStudent(StudentInfoReqDTO studentInfoReqDTO) {
        StudentInfo studentInfo = new StudentInfo();
        BeanUtils.copyProperties(studentInfoReqDTO,studentInfo);
        return this.updateById(studentInfo);
    }

    @Override
    public IPage<StudentInfoRespDTO> paging(PagingReqDTO<StudentInfoReqDTO> reqDTO) {
        return baseMapper.paging(reqDTO.toPage(),reqDTO.getParams());
    }

    @Override
    public StudentInfoRespDTO getDetail(BaseIdReqDTO baseIdReqDTO) {
        StudentInfoRespDTO studentInfoRespDTO = new StudentInfoRespDTO();
        StudentInfo studentInfo = this.getById(baseIdReqDTO.getId());
        BeanUtils.copyProperties(studentInfo, studentInfoRespDTO);
        LocalDate dob = studentInfo.getDob();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        studentInfoRespDTO.setDob(dob.format(formatter));
        return studentInfoRespDTO;
    }

    @Override
    public void importExce(String id, List<StudentExportDTO> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        // 批量插入
        list.forEach(item ->{
            //题目基本信息
            StudentInfoReqDTO studentInfoReqDTO = new StudentInfoReqDTO();
            BeanUtils.copyProperties(item,studentInfoReqDTO);
            studentInfoReqDTO.setTrainingClassInfoId(id);
            this.add(studentInfoReqDTO);
        });
    }

    @Override
    public StudentInfoExportDTO getStudentPdfData(String trainingClassInfoId) {
        StudentInfoExportDTO studentInfoExportDTO = new StudentInfoExportDTO();

        List<StudentInfoListExportDTO> studentPdfDataList = baseMapper.getStudentPdfDataList(trainingClassInfoId);
        studentInfoExportDTO.setStudentInfoListExportDTOList(studentPdfDataList);
        return studentInfoExportDTO;
    }

    @Override
    @SneakyThrows
    public List<StudentAssessmentExportDTO> getStudentExamPdfData(String trainingClassInfoId) {
        return baseMapper.getStudentExamPdfData(trainingClassInfoId);
    }
}
